package com.my.cd.decorator2;

/**
 * 窗口装饰器
 */
public class WindowDecorate {

    public static void main(String[] args) {
        Window window = new SimpleWindow();
        window = new VerticalScrollBarDecorator(window);
        window = new HorizontalScrollBarDecorator(window);

        window.draw();
        System.out.println(window.getDescription());
    }

    interface Window {
        void draw(); // draws
        String getDescription(); //说明
    }

    static class SimpleWindow implements Window {

        public void draw() {
            System.out.println("draw window");
        }

        public String getDescription() {
            return "simple window";
        }

    }

    static abstract class WindowDecorator implements Window {
        protected Window decoratedWindow; //要装饰的窗体

        public WindowDecorator(Window decoratedWindow) {
            this.decoratedWindow = decoratedWindow;
        }
    }

    static class HorizontalScrollBarDecorator extends WindowDecorator {
        public HorizontalScrollBarDecorator(Window decoratedWindow) {
            super(decoratedWindow);
        }

        public void draw() {
            decoratedWindow.draw();
            drawHorizontalScrollBar();
        }

        private void drawHorizontalScrollBar() {
            System.out.println("draw the horizontal scrollbar");
        }

        public String getDescription() {
            return decoratedWindow.getDescription() + ", including horizontal scrollbars";
        }
    }

    static class VerticalScrollBarDecorator extends WindowDecorator {
        public VerticalScrollBarDecorator(Window decoratedWindow) {
            super(decoratedWindow);
        }

        public void draw() {
            decoratedWindow.draw();
            drawVerticalScrollBar();
        }

        private void drawVerticalScrollBar() {
            System.out.println("draw the vertical scrollbar");
        }

        public String getDescription() {
            return decoratedWindow.getDescription() + ", including vertical scrollbars";
        }
    }

}
